package br.com.yurikimo.ykkit;

import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class YKTable extends ScrollView
{
	private TableLayout tableLayout;
	private RelativeLayout.LayoutParams layoutParams;
	
	private float x = 0;
	private float y = 0;
	private float width = 100;
	private float height = 26;
	private RelativeLayout relativeLayout = null;
	
  public YKTable() {
      super(YKDirector.getActivity());        

      tableLayout = new TableLayout(YKDirector.getActivity());
      tableLayout.setShrinkAllColumns(true);      
      
      addView(tableLayout);
  }
  
  public void setBackground(int imageID){
  	setBackgroundResource(imageID);
  }
  
  public void addRow(TableRow tableRow){
  	tableLayout.addView(tableRow);
  }
  
  public void removeRow(TableRow tableRow){
  	tableLayout.removeView(tableRow);
  }
  
  public void setBounds(float x, float y, float width, float height)
	{
		layoutParams = null;
		
		layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
		
		layoutParams.leftMargin = (int)x;
		layoutParams.topMargin = (int)y;				
	}
  
  public void addToLayout(RelativeLayout relativeLayout)
	{
		this.relativeLayout = relativeLayout;
		
		relativeLayout.addView(this, layoutParams);
		relativeLayout.invalidate();
	}
  
  public float getTableWidth()
	{
		return width;
	}
	
	public float getTableHeight()
	{
		return height;
	}
	
	private void refreshLayout()
	{
		setBounds(x, y, width, height);
		
		if (relativeLayout != null)
		{
			if (relativeLayout.indexOfChild(this) >= 0)
			{
				relativeLayout.removeView(this);
				
				relativeLayout.addView(this, layoutParams);
				relativeLayout.invalidate();
			}
		}
	}
	
	public void setPosition(float x, float y)
	{
		this.x = x;
		this.y = y;
	
		refreshLayout();
	}
}

/*
  
      for (int i = 0; i < 20; i++){
      	TableRow tableRow = new TableRow(context);
      	tableRow.setBackgroundColor(Color.RED);
      	
      	Button button = new Button(context);
      	button.setText("Button " + (i + 1));
      	
      	tableRow.addView(button);
      }
 */
